package com.financing.wap.web.controller.adminapi;

import com.financing.wap.common.CommonConstantUtil.CommonStatus;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.DstMemberInfoDTO;
import com.financing.wap.dto.DstUserIntegralFlowDTO;
import com.financing.wap.dto.DstUserWithdrawLogDTO;
import com.financing.wap.service.IDstMemberInfoService;
import com.financing.wap.service.IDstUserIntegralFlowService;
import com.financing.wap.service.IDstUserWithdrawLogService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.DateUtil;
import com.financing.wap.web.controller.BaseController;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Controller("adminDstUserWithdrawLogController")
@RequestMapping("/adminapi/dstUserWithdrawLog")
public class DstUserWithdrawLogController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IDstUserWithdrawLogService dstUserWithdrawLogService;
	
	@Autowired
	private IDstMemberInfoService dstMemberInfoService;

	@Autowired
	private IDstUserIntegralFlowService dstUserIntegralFlowService;
	
	@RequestMapping("/listDstUserWithdrawLog")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, DstUserWithdrawLogDTO dstUserWithdrawLog,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query admin dstUserWithdrawLog list begin .......");
			
			dstUserWithdrawLog.setBegin(pager.getStartIndex());
			dstUserWithdrawLog.setPageSize(pager.getPageSize());
			dstUserWithdrawLog.setWithdrawType(CommonStatus.WITHDRAW_TYPE_2);
			dstUserWithdrawLog.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
			
			int count = dstUserWithdrawLogService.queryListCount(dstUserWithdrawLog);
			
			pager.setTotalCount(count);
			
			List<DstUserWithdrawLogDTO> dstUserWithdrawLogList = null;
			
			if(count > 0){
				dstUserWithdrawLogList = dstUserWithdrawLogService.queryList(dstUserWithdrawLog);
			}else{
				dstUserWithdrawLogList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", dstUserWithdrawLogList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query admin dstUserWithdrawLog list end .......");
		}catch(Exception e){
			log.error("query admin dstUserWithdrawLog error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	@RequestMapping("/updateDstUserWithdrawLog")
	@ResponseBody
	public ResultMap updateDstUserWithdrawLog(HttpServletRequest request, DstUserWithdrawLogDTO dstUserWithdrawLog){
		ResultMap result = new ResultMap();

		try{
			logger.info("query admin updateDstUserWithdrawLog list begin .......");

			Integer id = dstUserWithdrawLog.getId();

			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			DstUserWithdrawLogDTO dstUserWithdrawLogDTO = dstUserWithdrawLogService.loadById(id);
			if(dstUserWithdrawLogDTO.getId()==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			DTOdateSetUtil.setUpdateInfo(dstUserWithdrawLog);

			if(CommonStatus.WITHDRAW_STATUS_3.equals(dstUserWithdrawLog.getWithdrawStatus())){
				Date now = new Date();
				//1、积分记录表
				DstUserIntegralFlowDTO dstUserIntegralFlow =  new DstUserIntegralFlowDTO();
				dstUserIntegralFlow.setCreateDate(now);
				dstUserIntegralFlow.setUpdateDate(now);
				dstUserIntegralFlow.setCreateBy("admin");
				dstUserIntegralFlow.setUpdateBy("admin");
				dstUserIntegralFlow.setUserOpenid(dstUserWithdrawLogDTO.getUserOpenid());
				dstUserIntegralFlow.setType("return_"+dstUserWithdrawLogDTO.getGoodsType());
				dstUserIntegralFlow.setIntegralStatus("2");
				dstUserIntegralFlow.setBusinessId(dstUserWithdrawLogDTO.getId());
				dstUserIntegralFlow.setGrade(dstUserWithdrawLogDTO.getUserWithdrawMoney().intValue());
				dstUserIntegralFlow.setBudget("1");
				dstUserIntegralFlow.setIntegralDate(DateUtil.getDate("yyyy-MM-dd"));
				dstUserIntegralFlowService.insert(dstUserIntegralFlow);

				logger.info("user admin dstUserIntegralFlowService insert");

				//更新使用积分
				DstMemberInfoDTO dstMemberInfo = new DstMemberInfoDTO();
				dstMemberInfo.setUserOpenid(dstUserWithdrawLogDTO.getUserOpenid());
				dstMemberInfo = dstMemberInfoService.queryListGetFirst(dstMemberInfo);
				dstMemberInfo.setMemberUseScore(dstMemberInfo.getMemberUseScore() == null?0:dstMemberInfo.getMemberUseScore()  - dstUserWithdrawLogDTO.getUserWithdrawMoney().intValue());
				dstMemberInfoService.update(dstMemberInfo);

				logger.info("user admin dstMemberInfoService update");
			}

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			dstUserWithdrawLogService.update(dstUserWithdrawLog);

			logger.info("query admin dstUserWithdrawLogService end .......");
		}catch(Exception e){
			log.error("query admin dstUserWithdrawLog error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}
}
